<?php

/*
 * Shopping Cart class and related methods
 */

class Cart {

  private $cartId;
  private $cartItems;

  public function getCartId() {
    return $this->cartId;
  }

  public function setCartId($cartId) {
    $this->cartId = $cartId;
  }

  public function getCartItems() {
    return $this->cartItems;
  }

  public function setCartItems($cartItems) {
    $this->cartItems = $cartItems;
  }

 public function updateItem($productId, $quantity) {
   $this->addItem($productId, $quantity, true);
 }

  public function addItem($productId, $quantity, $updateFlag=false) {
    if (!empty($productId) && !empty($quantity) ) {
      $product = Product::getProduct($productId);
      if(!empty($product)){
        if ($this->hasProduct($productId)) {
          $cartItems = $this->getCartItems();
          if(!empty($cartItems)){
            foreach ($cartItems as $item) {
              if ($item->getProduct()->getProductId()===$productId){
                if(empty($updateFlag)) {
                  $item->setQuantity((int)$item->getQuantity() + (int)$quantity);
                } else {
                  $item->setQuantity($quantity);
                }
                $this->setCartItems($cartItems);
                break;
              }
            }
          }
        } else {  // not present in cart
          $cartItems = $this->getCartItems();
          $cartItems = !empty($cartItems) ? $cartItems : array();
          array_push($cartItems, new CartItem($product, $quantity));
          $this->setCartItems($cartItems);
        }
      }

    }
  }

  public function removeItem($productId) {
    if ($this->hasProduct($productId)) {
      $count=0;
      $cartItems = $this->getCartItems();
      foreach($cartItems as $cartItem) {
        if ($cartItem->getProduct()->getProductId()===$productId) {
          array_splice($cartItems, $count, 1);
          $this->setCartItems($cartItems);
          break;
        }
        $count++;
      }
    }
  }

  public function emptyCart() {
    $this->setCartItems(null);
  }

  public function getCartTotal($tax=0) {
    $retVal = 0.00;
    $cartItems = $this->getCartItems();
    if(!empty($cartItems)){
      foreach ($cartItems as $cartItem){
        $retVal += (float)$cartItem->getProduct()->getPrice() * (int)$cartItem->getQuantity();
      }
    }
    $totalTax = (float)((100 + $tax)/100);
    return number_format((float) $retVal*$totalTax, 2);
  }

  public function hasProduct($productId){
    $retVal = false;
    if(!empty($productId)){
      $cartItems = $this->getCartItems();
      if(!empty($cartItems)){
       foreach ($cartItems as $item) :
         if($item->getProduct()->getProductId()===$productId) {
           $retVal = true;
           break;
         }
         endforeach;
      }
    }
    return $retVal;
  }

  public function getNumberOfProducts() {
    $retVal = 0;
    if(!empty($this->cartItems)) {
      foreach($this->cartItems as $cartItem) {
        $retVal += $cartItem->getQuantity();
      }
    }
    return $retVal;
  }

}

?>
